#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_IP "192.168.31.107"
#define SERVER_PORT 50000

int main(int argc, char const *argv[])
{
    int sockfd = socket( AF_INET, SOCK_STREAM, 0 );
    if ( -1 == sockfd )
    {
        perror( "fail socket" );
        return -1;
    }

    struct sockaddr_in siServer;
    siServer.sin_family = AF_INET;
    siServer.sin_port   = htons( SERVER_PORT );
    siServer.sin_addr.s_addr = inet_addr( SERVER_IP );
    
    int iRet;
    iRet = connect( sockfd, (struct sockaddr *)&siServer, sizeof(siServer) );
    if ( -1 == iRet )
    {
        perror( "fail connect" );
        return -1;
    }

    char strBuff[100] = "Hello first TCP";
    ssize_t szRet;
    szRet = send( sockfd, strBuff, strlen(strBuff) + 1, 0 );
    printf( "send #%ld bytes\n", szRet );

    close( sockfd );

    return 0;
}

/*
程序功能
    给指定的 TCP 服务器发送一个字符串. (Windows主机作为TCP服务器, 开启网络调试助手接收数据)

程序流程
    与TCP服务器建立链接
    发送数据
    关闭链接

涉及函数
    socket
    htons
    inet_addr
    connect
    send
    close

知识要点
*/
